package org.one.stone.soup.grfxML.plugin.text;

import org.one.stone.soup.grfxML.DataString;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.stringhelper.StringGenerator;

public class AttributeToString extends SimplePlugin {

	public static final int ARG_SOURCE_NAME = 0;
	public static final int ARG_SOURCE_ATTRIBUTE = 1;
	public static final int ARG_TARGET = 2;
	public static final int ARG_TEMPLATE = 3;

	private DataString sourceName = new DataString();
	private DataString sourceAttribute = new DataString();
	private DataString target = new DataString();
	private DataString template = new DataString();

	private Field field;
/**
 * FileToText constructor comment.
 */
public AttributeToString(GrfxMLEngine engine) {
	super(engine);
}
/**
 * initialize method comment.
 */
public void initialize()
{
	field = getEngine().getAPI().getField(sourceName.getValue());
}
/**
 * process method comment.
 */
public void process()
{
 try{
	String value = field.getValue(sourceAttribute.getValue());
	value = StringGenerator.generateStringWithMask( template.getValue(),new Object[]{value} );

	target.setValue(value,this);
 }catch(LockException le)
 {}
}
/**
 * register method comment.
 */
public void register(org.one.stone.soup.mjdb.data.field.DataLinker store)
{
	sourceName = grfxMLCaster.cast(sourceName,getArg(sourceName,ARG_SOURCE_NAME,store));
	sourceAttribute = grfxMLCaster.cast(sourceAttribute,getArg(sourceAttribute,ARG_SOURCE_ATTRIBUTE,store));
	target = grfxMLCaster.cast(target,getArg(target,ARG_TARGET,store));
	template = grfxMLCaster.cast(template,getArg(template,ARG_TEMPLATE,store));
}
public void replace(Field oldObj, Field newObj)
{
	sourceName = grfxMLCaster.replace(sourceName,oldObj,newObj);
	sourceAttribute = grfxMLCaster.replace(sourceAttribute,oldObj,newObj);
	target = grfxMLCaster.replace(target,oldObj,newObj);
	template = grfxMLCaster.replace(template,oldObj,newObj);
}
/**
 * stop method comment.
 */
public void stop() {}
}
